﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class SysManage_OrgTree_Print : System.Web.UI.Page
{
    BaiMaSoft.Conn.DataConn MyDataConn = new BaiMaSoft.Conn.DataConn();
    BaiMaSoft.SysManage.UserManage UMConn = new BaiMaSoft.SysManage.UserManage();
    public SqlConnection myconn;
    public SqlCommand mycomm, mycomm3;
    public SqlDataReader mydr;
    public SqlDataAdapter myadapter, myadapter2;
    public DataSet myds, myds2;
    public DataView mydv, mydv2;
    public DataTable mytb, mytb2;
    public DataRow myrow, myrow2;
    public DataColumn mycolumn, mycolumn2;
    public string filedtotal = "";
    public string filedtotal2 = "";
    public string sDeptHtml = "";
    public string sPersHtml = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (ViewState["VOrgType"] == null || ViewState["VOrgType"].ToString().CompareTo("") == 0)
            {
                ViewState["VOrgType"] = "0";
            }
            if (ViewState["VOrgIDCol"] == null || ViewState["VOrgIDCol"].ToString().CompareTo("") == 0)
            { //组织ID的集合
                ViewState["VOrgIDCol"] = "0";
            }

            try
            {
                HPageID.Value = Request["ChoiceID"].ToString().Trim();
            }
            catch
            {
                HPageID.Value = "0";
            }

            try
            {
                hOrgType.Value = Request.QueryString["OrgType"].ToString();
                ViewState["VOrgType"] = hOrgType.Value;
            }
            catch
            {
                hOrgType.Value = "2";
            }

            try
            {
                ViewState["VOrgIDCol"] = Request.QueryString["OrgIDCol"].ToString();
            }
            catch
            {
                ViewState["VOrgIDCol"] = "0";
            }


            string sIniOrgID = "0";
            IniDeptInfo(sIniOrgID);
        }
    }

    public void IniDeptInfo(string sSOrgID)
    {
        MyDataConn.Connection.Open();
        myadapter = new SqlDataAdapter("SELECT a.ID, a.Name, a.ParentID, a.ChildNumber, a.PersNumber, a.DOrder, a.Phone,a.Principal, b.Name AS PrincipalName, a.Fax, a.Effective, c.Name AS PrentName FROM Sys_Org_Other_Info a LEFT OUTER JOIN Sys_Pers_Info b ON a.Principal = b.ID LEFT OUTER JOIN Sys_Org_Other_Info c ON a.ParentID = c.ID WHERE (a.Effective = '1') and a.orgtype='" + ViewState["VOrgType"].ToString().Trim() + "' ORDER BY a.ParentID, a.DOrder", MyDataConn.Connection);
        myds = new DataSet();
        myadapter.Fill(myds, "Sys_Org_Other_Info");
        mytb = myds.Tables["Sys_Org_Other_Info"];

        foreach (DataColumn mycolumn in mytb.Columns)
        {
            filedtotal = filedtotal + mycolumn.ColumnName + ",";
        }

        sDeptHtml = sDeptHtml + "<script language=\"javascript\" type=\"text/javascript\">";
        filedtotal = filedtotal.Substring(0, filedtotal.Length - 1);
        sDeptHtml = sDeptHtml + " function tree(" + filedtotal + "){";
        string[] sfiledtotal = filedtotal.Split(',');
        int nfiledtotal = sfiledtotal.Length;
        for (int i = 0; i < nfiledtotal; i++)
        {
            sDeptHtml = sDeptHtml + "this." + sfiledtotal[i] + "=" + sfiledtotal[i] + ";";
        }

        if (mytb.Rows.Count > 0)
        {
            int iRowCount = mytb.Rows.Count - 1;
            sDeptHtml = sDeptHtml + "}var trees =new Array(" + iRowCount + ");";
        }
        else
        {
            sDeptHtml = sDeptHtml + "}var trees =new Array(0);";
        }

        for (int j = 0; j < mytb.Rows.Count; j++)
        {
            sDeptHtml = sDeptHtml + "trees[" + j + "] = new tree(";
            string temp = "";
            for (int k = 0; k < mytb.Columns.Count; k++)
            {
                temp = temp + "'" + mytb.Rows[j][k] + "'";
                if (k != mytb.Columns.Count - 1)
                {
                    temp = temp + ",";
                }
            }
            sDeptHtml = sDeptHtml + temp + ");";
        }
        sDeptHtml = sDeptHtml + "showtree(0,'" + ViewState["VOrgIDCol"].ToString().Trim() + "');";
        sDeptHtml = sDeptHtml + "</script>";
        sDeptHtml = sDeptHtml + "";
        MyDataConn.Connection.Close();
    }
}
